package com.dmx.fw.dao.hibernateimpl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.dmx.fw.bean.mapping.pojo.ParameterData;
import com.dmx.fw.bean.mapping.pojo.Resc;
import com.dmx.fw.bean.mapping.pojo.Role;
import com.dmx.fw.dao.interfaces.*;

public class RoleDaoImpl extends BaseDaoImpl implements RoleDao {

	public Role getRole(Long roleId) {
		return (Role)this.getObject(Role.class, Long.valueOf(roleId));
	}
	
	public List findRescByRoleId(Long roleId) {
		String hql = "select resc from Resc as resc inner join fetch resc.roles r where r.id=?";
		return getHibernateTemplate().find(hql, roleId);

	}

	public void save(Role role) {
		getHibernateTemplate().saveOrUpdate(role);
	}

	public void save(Role role, String[] funcArray) {
		Set<Resc> functions = new HashSet<Resc>();
		for (int i = 0; i < funcArray.length; i++) {
			Resc function = (Resc) getHibernateTemplate().get(Resc.class, Long.parseLong(funcArray[i]));
			functions.add(function);
		}
		role.setRescs(functions);
		getHibernateTemplate().saveOrUpdate(role);
	}

	public List findRole(Role role) {
		String hql = "from Role where name=?";
		Object[] temp = {role.getName()};
		return getHibernateTemplate().find(hql, temp);
	}

	public List findOperatorByRoleId(Long roleId) {
		String hql = "from Operator as optr inner join fetch optr.roles r where r.id=?";
		return getHibernateTemplate().find(hql, roleId);

	}

}
